<!--
 * @Description: 数据项管理页面
-->
<template>
  <!-- <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="171" @add="add" @del="del" />
    </template>
<template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">设备名称:</div>
          <div class="input">
            <el-input v-model="equipmentName" clearable />
          </div>
        </div>
      </SearchBox>
    </template>
<template v-slot:table-box>
      <div class="dataitem-main">
        <div class="tr-sid">
          <div class="sid-head">选择鱼塘</div>
          <div class="sid-tree">
            <treeSide @treeselectvalue="treeselectvalue" />
          </div>
        </div>
        <div class="tab-box">
          <div class="tab-content">
            <el-table ref="tableData" v-loading="loading" :data="tableData" stripe border height="calc(100% - 50px)"
              @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" :selectable="(row) => { return !row.selectable }" />
              <el-table-column type="index" label="序号" width="100" />
              <el-table-column prop="equipment.equipmentName" label="设备名称" />
              <el-table-column prop="equipment.equipmentTypeStr" label="设备类型" />
              <el-table-column width="120" label="操作">
                <template v-if="scope.row.fishPondEquipmentMapperId" slot-scope="scope">
                  <el-button v-if="judgePermission(171, 'delete')" class="text-btn-cursor cursor-delete" type="text"
                    size="small" @click="handleDelete(scope.row)">删除</el-button>
                </template>
</el-table-column>
</el-table>
<Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize" @changePageSize="changePageSize"
  @changeCurrentPage="changeCurrentPage" />
</div>
</div>
</div>
<el-dialog :close-on-click-modal='false' :title="'分配设备'" :visible.sync="dialogFormVisible" @close="cancel">
  <el-form ref="formData" :model="formData" label-position="left" class="w100p">
    <el-form-item label="设备列表" class="w100p checkbox">
      <el-select filterable clearable v-model="formData.equipmentIdList" multiple clearable filterable>
        <el-option v-for="item in equipmentList" :key="item.equipmentId + 'equipment'" :label="item.equipmentName"
          :value="item.equipmentId" />
      </el-select>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button class="cancelbtn" @click="cancel('formData')">取 消</el-button>
    <el-button class="savebtn" type="primary" @click="save">保 存</el-button>
  </div>
</el-dialog>
</template>
</BaseMainSlot> -->

  <div class="content">
    <div class="typeTree">
      <treeSide @treeselectvalue="treeselectvalue" />
    </div>
    <MainSlot>
      <template v-slot:searchForm>
        <SearchForm v-if="formItemList.length" :formItemList="formItemList" @search="search">
        </SearchForm>
      </template>
      <template v-slot:btn>
        <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
      </template>
      <template v-slot:table>
        <el-table ref="tableData" v-loading="loading" :data="tableData" stripe border height="calc(100% - 50px)"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" :selectable="(row) => { return !row.selectable }" />
          <el-table-column type="index" label="序号" width="100" />
          <el-table-column prop="equipment.equipmentName" label="设备名称" />
          <el-table-column prop="equipment.equipmentTypeStr" label="设备类型" />
          <el-table-column width="120" label="操作">
            <template v-if="scope.row.fishPondEquipmentMapperId" slot-scope="scope">
              <el-button class="text-btn-cursor cursor-delete" type="text" size="small"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:pagination>
        <Pagination @current-change="changeCurrentPage" :page="currentPage" :size="totalSize" :total="totalSize">
        </Pagination>
      </template>
      <template v-slot:dialogModule>
        <el-dialog :title="'分配设备'" :visible.sync="dialogFormVisible" @close="cancel" width="40%">
          <el-form ref="formData" :model="formData" label-width="120px" class="demo-ruleForm">
            <el-form-item label="设备列表">
              <el-select class="w100p" filterable clearable v-model="formData.equipmentIdList" multiple>
                <el-option v-for="item in equipmentList" :key="item.equipmentId" :label="item.equipmentName"
                  :value="item.equipmentId" />
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button class="cancelbtn" @click="cancel('formData')">取 消</el-button>
            <el-button class="savebtn" type="primary" @click="save">保 存</el-button>
          </div>
        </el-dialog>
      </template>
    </MainSlot>
  </div>
</template>

<script>

import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import BtnSlot from '@/components-production-management/BtnSlot';

import pondAllocationDeviceApi from "@/api/basic-info-manage/pond-allocation-device";
import DeviceManagementApi from "@/api/basic-info-manage/device-management";
import treeSide from "@/components/selectbutton/trelist.vue";

import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";
export default {
  name: "DataItemManagemente",
  components: {
    MainSlot,
    SearchForm,
    BtnSlot,
    Pagination,
    treeSide
  },
  mixins: [mymixin],
  data() {
    return {
      btnList: ['add', 'delete'],
      // 数据项类型id
      topitemtypeid: 1,
      // 设备名称
      equipmentName: "",
      // 鱼塘Id
      fishPondId: undefined,
      // 当前选中的数据项数组
      nowSelectList: [],
      equipmentList: [],
      loading: false,
      formItemList: [
        {
          label: '设备名称',
          type: 'input',
          param: 'equipmentName',
          multiple: false,
          isShow: true
        },
      ],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() { },
  mounted() {
    this.queryEquipment();
    // this.getTableDataList();
  },
  methods: {

    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.add();
      } else if (flag == 'delete') {
        this.delmessage();
      }
    },
    // 点击鱼塘触发方法
    treeselectvalue(e) {
      this.fishPondId = e;
      this.addActive = true;
      this.getTableDataList();
    },
    // 获取设备下拉选择框数据
    queryEquipment() {
      // 获取数据项选择项
      DeviceManagementApi.queryByCondition().then(res => {
        this.equipmentList = res.data;
      });
    },
    getTableDataList() {
      this.tableData = [];
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          equipmentName: this.equipmentName,
          fishPondId: this.fishPondId
        }
      };
      pondAllocationDeviceApi.queryPageByCondition(params).then(res => {
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
      });
    },
    search(data) {
      this.currentPage = 1;
      this.equipmentName = data.equipmentName;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleSelectionChange(list) {
      this.nowSelectList = [];
      let notvalue = true;
      this.nowSelectList = list.map(item => {
        if (item.fishPondEquipmentMapperId) {
          return item.fishPondEquipmentMapperId;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.delActive = false;
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.nowSelectList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      if (!this.nowSelectList.length) return;
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        pondAllocationDeviceApi.delListById({ idList: this.nowSelectList }).then(res => {
          if (res.success) {
            this.$message.success("删除成功！");
            this.getTableDataList();
          } else {
            this.$message.error("删除失败！");
          }
        });
      })
        .catch(() => {
          this.$message("取消删除");
        });

    },
    handleDelete(data) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          pondAllocationDeviceApi
            .delById({ id: data.fishPondEquipmentMapperId })
            .then(res => {
              if (res.success) {
                this.$message.success("删除成功");
                this.getTableDataList();
              } else {
                this.$message.error("删除失败");
              }
            });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save() {
      if (this.nowChanceStau === "edit") {
        pondAllocationDeviceApi.editlist(this.formData).then(res => {
          if (res.success) {
            this.$message.success("修改成功！");
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === "add") {
        if (this.fishPondId) this.formData.fishPondId = this.fishPondId;
        // if(this.equipmentIdList) this.formData.equipmentIdList = this.equipmentIdList;
        pondAllocationDeviceApi
          .addEquipmentToFishPond(this.formData)
          .then(res => {
            if (res.success) {
              this.$message.success("新增成功！");
              this.getTableDataList();
            } else {
              this.$message.error("新增失败！");
            }
          });
      }
    }
  }
};
</script>

<style scoped lang="scss">
.content {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;

  .typeTree {
    width: 300px;
  }

  .main {
    width: calc(100% - 300px);

    .tableSlot {
      height: 100%;
    }
  }
}
</style>
